package no.nordicsemi.android.nrfmesh.utils;

import android.util.Log;
import no.nordicsemi.android.nrfmesh.domain.DeviceStatus;

/**
 * 硬件数据调试工具类
 * 用于诊断硬件数据接收和处理过程中的问题
 */
public class HardwareDataDebugger {
    private static final String TAG = "HardwareDataDebugger";
    
    /**
     * 调试设备状态数据
     */
    public static void debugDeviceStatus(DeviceStatus deviceStatus, String context) {
        Log.d(TAG, "=== 设备状态调试信息 [" + context + "] ===");
        
        if (deviceStatus == null) {
            Log.w(TAG, "⚠️ DeviceStatus 为 null！这是导致数据保存为零的主要原因");
            return;
        }
        
        Log.d(TAG, "✅ DeviceStatus 不为 null");
        Log.d(TAG, "开关状态: " + deviceStatus.getOnoff());
        Log.d(TAG, "电压0: " + deviceStatus.getVoltage0() + " (原始值)");
        Log.d(TAG, "电压1: " + deviceStatus.getVoltage1() + " (原始值)");
        Log.d(TAG, "电流: " + deviceStatus.getCurrent() + " (原始值)");
        Log.d(TAG, "阻抗: " + deviceStatus.getResistance() + " (原始值)");
        Log.d(TAG, "电量: " + deviceStatus.getSoc() + "%");
        Log.d(TAG, "档位: " + deviceStatus.getGear());
        
        // 计算转换后的值
        float voltage0InV = (float) deviceStatus.getVoltage0() / 1000;
        float voltage1InV = (float) deviceStatus.getVoltage1() / 1000;
        float currentInA = (float) deviceStatus.getCurrent() / 1000;
        float resistanceInOhm = (float) deviceStatus.getResistance() * 1000;
        
        Log.d(TAG, "转换后的值:");
        Log.d(TAG, "电压0: " + voltage0InV + " V");
        Log.d(TAG, "电压1: " + voltage1InV + " V");
        Log.d(TAG, "电流: " + currentInA + " A");
        Log.d(TAG, "阻抗: " + resistanceInOhm + " Ω");
        Log.d(TAG, "=== 调试信息结束 ===");
    }
    
    /**
     * 调试JSON解析过程
     */
    public static void debugJsonParsing(String json, String context) {
        Log.d(TAG, "=== JSON解析调试 [" + context + "] ===");
        Log.d(TAG, "接收到的JSON: " + json);
        
        if (json == null || json.trim().isEmpty()) {
            Log.w(TAG, "⚠️ JSON为空或null");
            return;
        }
        
        // 检查JSON格式
        if (!json.trim().startsWith("{") || !json.trim().endsWith("}")) {
            Log.w(TAG, "⚠️ JSON格式可能不正确，不是以{}包围");
        }
        
        Log.d(TAG, "JSON长度: " + json.length());
        Log.d(TAG, "=== JSON调试结束 ===");
    }
    
    /**
     * 调试数据收集过程
     */
    public static void debugDataCollection(float voltage1, float voltage2, float current, float impedance, String context) {
        Log.d(TAG, "=== 数据收集调试 [" + context + "] ===");
        Log.d(TAG, "收集到的数据:");
        Log.d(TAG, "电压1: " + voltage1 + " V");
        Log.d(TAG, "电压2: " + voltage2 + " V");
        Log.d(TAG, "电流: " + current + " A");
        Log.d(TAG, "阻抗: " + impedance + " Ω");
        
        // 检查是否全为零
        if (voltage1 == 0 && voltage2 == 0 && current == 0 && impedance == 0) {
            Log.w(TAG, "⚠️ 所有数据都为零！这表明latestDeviceStatus可能为null");
        } else {
            Log.d(TAG, "✅ 至少有一些非零数据");
        }
        Log.d(TAG, "=== 数据收集调试结束 ===");
    }
}